﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Generic.Util;
using WuDada.Core.Post.Domain;

public partial class admin_UC06_0601 : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IMessageService m_MessageService;
    private SessionHelper m_SessionHelper = new SessionHelper();
    private WebMailService m_WebMailService = new WebMailService();

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_MessageService = m_PostFactory.GetMessageService();

        if (!Page.IsPostBack)
        {
            pnlContent.Visible = false;
            fillGridView();
        }
    }

    private void ShowMode()
    {
        if (m_Mode == 0)
        {
            btnReply.Visible = false;
        }
        else
        {
            btnReply.Visible = true;
        }
    }

    protected void btnQuery_Click(object sender, ImageClickEventArgs e)
    {
        fillGridView();
    }

    private void fillGridView()
    {
        //搜尋條件
        DateTime? startDate = ConvertUtil.ToDateTimeMin(ConvertUtil.ToDateTime(txtDateFrom.Text));
        DateTime? endDate = ConvertUtil.ToDateTimeMax(ConvertUtil.ToDateTime(txtDateTo.Text));
        string sortField = "CreatedDate";
        bool sortDesc = true;

        //分頁
        AspNetPager1.RecordCount = m_MessageService.CountMessage(string.Empty, startDate, endDate);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        gvDetail.DataSource = m_MessageService.GetMessageList(string.Empty, startDate, endDate, pageIndex, pageSize, sortField, sortDesc);
        gvDetail.DataBind();
    }

    private void ClearUI()
    {
        m_Mode = 0;
        pnlContent.Visible = false;
        UIHelper.ClearUI(pnlContent);
    }

    protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int messageId = int.Parse(e.CommandArgument.ToString());
        MessageVO messageVO = m_MessageService.GetMessageById(messageId);

        ClearUI();
        m_Mode = messageId;
        UIHelper.FillUI(pnlContent, messageVO);
        ShowMode();
        pnlContent.Visible = true;
        switch (cmdName)
        {
            case "myReply":
                txtReplyContent.ReadOnly = false;
                btnReply.Visible = true;
                break;
            case "myShow":
                btnReply.Visible = false;
                txtReplyContent.ReadOnly = true;
                break;
            default:
                break;
        }
        //fillGridView();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
    }

    protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView gv = (GridView)sender;
        if (e.Row.RowIndex != -1)
        {
            Control ctrl = e.Row;
            MessageVO messageVO = (MessageVO)e.Row.DataItem;

            bool replyDisplay = string.IsNullOrEmpty(messageVO.ReplyBy);

            UIHelper.SetContrlVisible(ref ctrl, "btnReply", replyDisplay);
            UIHelper.SetContrlVisible(ref ctrl, "btnShow", !replyDisplay);

            //string replyDisplay = !string.IsNullOrEmpty(messageVO.ReplyBy) ? "display:none;" : string.Empty;
            //string detailDisplay = string.IsNullOrEmpty(messageVO.ReplyBy) ? "display:none;" : string.Empty;

            //string replyFun = string.Format("<a id='Reply{0}' href='javascript:ShowReply({0})' style='{1}'>回覆</a>", messageId, replyDisplay);
            //string replyDetailFun = string.Format("<a id='ReplyContent{0}' href='javascript:ShowReplyContent({0})' style='{1}'>顯示回覆內容</a>", messageId, detailDisplay);
            //UIHelper.SetLiteralText(ctrl, "ltlReply", (replyFun + replyDetailFun));
        }        
    }

    protected void btnReply_Click(object sender, EventArgs e)
    {
        MessageVO messageVO = m_MessageService.GetMessageById(m_Mode);
        messageVO.ReplyContent = txtReplyContent.Text.Trim();
        if (!string.IsNullOrEmpty(messageVO.ReplyContent))
        {
            messageVO.ReplyDate = DateTime.Now;
            messageVO.ReplyBy = m_SessionHelper.LoginUser.UserId;
            
            ////送信給留言人
            m_WebMailService.SendMail_ToCreateUser_ByMessage(messageVO);

            m_MessageService.UpdateMessage(messageVO);
            fillGridView();
            ClearUI();
            ShowMode();

            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("已送出回覆信。"), false);
        }
    }
}